<?php
namespace Admin\Controller;

use Common\Controller\AdminBaseController;

/**
 * 文章
 */
class ArticleController extends AdminBaseController
{
    /**
     * 文章列表
     */
    public function index()
    {
        $this->list_article();
    }

    public function list_article()
    {
        // $result = M('article')->field('*')->select();
        if ($_GET['ac_id']) {
            $condition['ac_id'] = $_GET['ac_id'];

        }
        if ($_GET['article_title']) {
            $condition['like_title'] = $_GET['article_title'];
        }
        if ($_GET['strtime'] || $_GET['endtime']) {
            $condition['strtime'] = $_GET['strtime'];
            $condition['endtime'] = $_GET['endtime'];
        }

        $data = D('Article')->getArticleList($condition, '20');

        if (is_array($data['data'])) {
            /**
             * 取文章分类
             */
            $model_class = D('ArticleClass');
            $class_list  = $model_class->getClassList($condition);

            $tmp_class_name = array();
            if (is_array($class_list)) {
                foreach ($class_list as $k => $v) {
                    $tmp_class_name[$v['ac_id']] = $v['ac_name'];
                }
            }

            foreach ($data['data'] as $k => $v) {
                /**
                 * 发布时间
                 */
                $data['data'][$k]['article_time'] = date('Y-m-d H:i:s', $v['article_time']);
                /**
                 * 所属分类
                 */
                if (@array_key_exists($v['ac_id'], $tmp_class_name)) {
                    $data['data'][$k]['ac_name'] = $tmp_class_name[$v['ac_id']];
                }
            }
        }
        $model_class = D('ArticleClass');
        $parent_list = $model_class->getTreeClassList(2);

        if (is_array($parent_list)) {
            $unset_sign = false;
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
            }
        }
        $this->assign('parent_list', $parent_list);
        // print_r($data);exit;
        $this->assign('article', $data);
        $this->display('index');
    }

    /**
     * 文章添加
     */
    public function article_add()
    {
        $model_article = D('Article');

        /**
         * 保存
         *
         * 文章图 banner图的配图  用max查询的话一旦之前删除过记录导致id不连贯  后面一条新增的时候就会出错
         */

        if (IS_POST) {
            $insert_array                     = array();
            $insert_array['article_title']    = trim($_POST['article_title']);
            $insert_array['ac_id']            = intval($_POST['ac_id']);
            $insert_array['article_url']      = trim($_POST['article_url']);
            $insert_array['article_abstract'] = trim($_POST['article_abstract']);
            $insert_array['article_author']   = trim($_POST['article_author']);
            $insert_array['article_source']   = trim($_POST['article_source']);
            $insert_array['article_show']     = trim($_POST['article_show']);
            $insert_array['article_sort']     = trim($_POST['article_sort']);
            $insert_array['article_content']  = trim($_POST['editorValue']);
            $insert_array['article_time']     = time();
            if ($_FILES['article_img']['error'] == 0) {
                $m_article_id = M('article')->max('article_id');
                $article_id   = $m_article_id + 1;
                $config       = array(
                    'rootPath' => './data/',
                    'savePath' => 'upload/article/',
                    'maxSize'  => 512000, //500K
                    // 'saveName'   =>    array('date','Ym'),
                    'saveName' => 'article_' . intval($article_id),
                    'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                    'autoSub'  => false,
                );
                $upload = new \Think\Upload($config); // 实例化上传类
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['article_img']);
                //$info =$upload->upload();
                if (!$info) {
// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
// 上传成功 获取上传文件信息
                    $insert_array['article_img'] = $info['savename'];
                }
            }

            $result = $model_article->add($insert_array);
            if ($result) {
                admin_log('添加文章' . '[' . $_POST['article_title'] . ']', 1);
                $this->success('添加文章成功', 'index');exit;
            } else {
                $this->error('添加文章失败', 'index');exit;
            }

        }
        /**
         * 分类列表
         */
        $model_class = D('ArticleClass');
        $parent_list = $model_class->getTreeClassList(2);
        if (is_array($parent_list)) {
            $unset_sign = false;
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
            }
        }

        $this->assign('ac_id', intval($_GET['ac_id']));

        $this->assign('parent_list', $parent_list);
        $this->display('article.add');
    }

    /**
     * 文章编辑
     */
    public function article_edit()
    {

        $model_article = D('Article');
        $id            = $_GET['id'];
        if (IS_POST) {
            $update_array                     = array();
            $update_array['article_id']       = intval($_POST['article_id']);
            $update_array['article_title']    = trim($_POST['article_title']);
            $update_array['ac_id']            = intval($_POST['ac_id']);
            $update_array['article_url']      = trim($_POST['article_url']);
            $update_array['article_abstract'] = trim($_POST['article_abstract']);
            $update_array['article_author']   = trim($_POST['article_author']);
            $update_array['article_source']   = trim($_POST['article_source']);
            $update_array['article_show']     = trim($_POST['article_show']);
            $update_array['article_sort']     = trim($_POST['article_sort']);
            $update_array['article_content']  = trim($_POST['editorValue']);

            if ($_FILES['article_img']['error'] == 0) {

                $article_array = $model_article->getOneArticle(intval($_POST['article_id']));
                if (!empty($article_array['article_img'])) {
                    unlink('./data/upload/article/' . $article_array['article_img']);
                }
                $config = array(
                    'rootPath' => './data/',
                    'savePath' => 'upload/article/',
                    'maxSize'  => 2048000, //500K
                    // 'saveName'   =>    array('date','Ym'),
                    'saveName' => 'article_' . intval($_POST['article_id']),
                    'exts'     => array('jpg', 'gif', 'png', 'jpeg'),
                    'autoSub'  => false,
                );
                $upload = new \Think\Upload($config); // 实例化上传类
                // 上传单个文件
                $info = $upload->uploadOne($_FILES['article_img']);

                if (!$info) {
// 上传错误提示错误信息
                    $this->error($upload->getError());
                } else {
// 上传成功 获取上传文件信息

                    $update_array['article_img'] = $info['savename'];

                }
            }
            //print_r($update_array);exit;

            $result = $model_article->update($update_array);
            if ($result) {
                admin_log('编辑文章[' . $_POST['article_title'] . ']', 1);
                $this->success('编辑文章成功', 'index');exit;
            } else {
                $this->error('编辑文章失败', 'index');exit;
            }
        }

        $article_array = $model_article->getOneArticle(intval($_GET['id']));
        if (empty($article_array)) {
            $this->error('参数错误', 'index');exit;
        }

        /**
         * 父类列表，只取到第一级
         */
        $model_class = D('ArticleClass');
        $parent_list = $model_class->getTreeClassList(2);
        if (is_array($parent_list)) {
            $unset_sign = false;
            foreach ($parent_list as $k => $v) {
                $parent_list[$k]['ac_name'] = str_repeat("&nbsp;", $v['deep'] * 2) . $v['ac_name'];
            }
        }

        $this->assign('parent_list', $parent_list);
        $this->assign('article_id', $id);
        $this->assign('article_array', $article_array);
        $this->display('article.edit');
    }
    /**
     * ajax操作
     */
    public function article_del()
    {

        $id            = $_POST['article_id'];
        $model_article = D('Article');
        $article_array = $model_article->getOneArticle(intval($id));

        $info = $model_article->del($id);
        if ($info) {
            admin_log('删除文章' . '[' . $id . ']' . $article_array['article_title'], 1);
            unlink('./data/upload/article/' . $article_array['article_img']);
            echo 1;exit;
        }

    }
    public function detadel()
    {
        $model_article = D('Article');

        $article_id = $_POST['article_id'];

        foreach ($article_id as $key => $val) {
            $article_array = $model_article->getOneArticle(intval($val));
            unlink('./data/upload/article/' . $article_array['article_img']);
            $info = $model_article->del($val);
            admin_log('批量删除文章' . 'ID[' . $val . ']' . $article_array['article_title'], 1);
        }
        if ($info) {

            $this->success('删除完成', 'index');exit;
        } else {
            $this->error('删除失败', 'index');exit;
        }

    }
    //修改发布下架状态
    public function article_show()
    {
        $model_article = D('Article');

        if ($_POST['id']) {
            $update_array['article_id']   = $_POST['id'];
            $update_array['article_show'] = '1';

        }
        if ($_POST['article_id']) {
            $update_array['article_show'] = '0';
            $update_array['article_id']   = $_POST['article_id'];
        }
        $info = $model_article->update($update_array);
        if ($info) {
            admin_log('修改文章发布状态ID[' . $update_array['article_id'] . ']', 1);
            $data = array('info' => '修改成功', 'status' => '1');
            echo json_encode($data);exit;
        } else {
            $data = array('info' => '修改失败', 'status' => '0');
            echo json_encode($data);exit;
        }

    }

    /**
     * 文章分类
     */
    public function cat()
    {
        $model_article = D('ArticleClass');
        if ($_POST['ac_name']) {
            $condition['ac_name'] = $_POST['ac_name'];
        } else {
            $condition['ac_parent_id'] = '0';
        }
        $model_class = $model_article->getClassList($condition, $page);
        //print_r($model_class);exit;
        foreach ($model_class as $key => $val) {

            $condition['ac_parent_id'] = $val['ac_id'];

            $model_class[$key]['parent'] = $model_article->getClassList($condition);

        }

        // print_r($model_class);exit;

        $this->assign('parent_list', $model_class);

        $this->display();
    }
/**
 *
 */
    public function edit_cat_sort()
    {
        $data          = array('info' => '修改失败', 'status' => '0');
        $model_artilce = D('ArticleClass');
        if (!isAJAX) {
            halt('请求有误');

        } else {
            $ac_id   = I('ac_id');
            $ac_sort = I('ac_sort');
            if (!empty($ac_id)) {
                $update_array['ac_id']   = $ac_id;
                $update_array['ac_sort'] = $ac_sort;
                $info                    = $model_artilce->update($update_array);
                if ($info >= 0) {
                    admin_log('编辑文章分类排序[ID:' . intval($ac_id) . ']', 1);
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            //var_dump($data);exit;
            $this->ajaxReturn($data, 'json');
        }
    }
    public function edit_cat_name()
    {
        $data          = array('info' => '修改失败', 'status' => '0');
        $model_artilce = D('ArticleClass');
        if (!isAJAX) {
            halt('请求有误');

        } else {
            $ac_id   = I('ac_id');
            $ac_name = I('ac_name');
            if (!empty($ac_id)) {
                $update_array['ac_id']   = $ac_id;
                $update_array['ac_name'] = $ac_name;
                $info                    = $model_artilce->update($update_array);
                if ($info >= 0) {
                    admin_log('编辑文章分类名称[ID:' . intval($ac_id) . ']' . $ac_name, 1);
                    $data = array('info' => '修改成功', 'status' => '1');
                } else {
                    $data = array('info' => '修改失败', 'status' => '0');
                }
            } else {
                $data = array('info' => '参数有误');
            }
            //var_dump($data);exit;
            $this->ajaxReturn($data, 'json');
        }
    }

    public function article_cat_add()
    {
        $model_class = D('ArticleClass');
        if (IS_POST) {
            $insert_array                 = array();
            $insert_array['ac_parent_id'] = trim($_POST['ac_parent_id']);
            $insert_array['ac_name']      = trim($_POST['ac_name']);
            $insert_array['ac_sort']      = trim($_POST['ac_sort']);
            $insert_array['ac_code']      = trim($_POST['ac_code']);

            $result = $model_class->add($insert_array);
            if ($result) {
                //admin_log(L('article_edit_succ').'['.$_POST['article_title'].']',null);
                admin_log('添加文章分类' . '[' . $_POST['ac_name'] . ']', null);
                $this->success('添加文章分类成功', 'cat');exit;
            } else {
                $this->error('添加文章分类失败', 'cat');exit;
            }
        }

        /**
         * 父类列表，只取到第一级
         */

        $parent_list = $model_class->getTreeClassList(1);

        $this->assign('parent_list', $parent_list);
        $this->display();
    }

    public function article_cat_edit()
    {
        $model_class = D('ArticleClass');
        $id          = $_GET['id'];
        if (IS_POST) {
            $update_array                = array();
            $update_array['parent_list'] = trim($_POST['parent_list']);
            $update_array['ac_id']       = trim($_POST['ac_id']);
            $update_array['ac_name']     = trim($_POST['ac_name']);
            $update_array['ac_sort']     = trim($_POST['ac_sort']);
            $update_array['ac_code']     = trim($_POST['ac_code']);

            $result = $model_class->update($update_array);
            if ($result) {
                admin_log('编辑文章分类' . '[' . $_POST['ac_name'] . ']', null);
                $this->success('编辑文章分类成功', 'cat');exit;
            } else {
                $this->error('编辑文章分类失败', 'cat');exit;
            }
        }

        /**
         * 父类列表，只取到第一级
         */
        $articleact = $model_class->getOneClass($id);

        $parent_list = $model_class->getTreeClassList(1);
        /*if (is_array($parent_list)){
        $unset_sign = false;
        foreach ($parent_list as $k => $v){
        $parent_list[$k]['ac_name'] = str_repeat("&nbsp;",$v['deep']*2).$v['ac_name'];
        }
        }*/

        $this->assign('parent_list', $parent_list);
        $this->assign('articleact', $articleact);
        $this->assign('ac_id', $id);
        $this->display();
    }

    /**
     * ajax操作
     */
    public function articleclass_del()
    {
        $data = array('info' => '删除失败', 'status' => '0');
        if (!isAJAX) {
            halt('请求有误');
        } else {
            $ac_id = I('ac_id');

            if (!empty($ac_id)) {
                $cat_id  = M('article_class')->where('ac_parent_id=' . $ac_id)->select();
                $article = M('article')->where('ac_id=' . $ac_id)->select();
                if ($cat_id || $article) {
                    //var_dump($cat_id);exit;
                    $data = array('info' => '该分类下还有二级分类或文章，不能直接删除', 'status' => '0');
                } else {
                    $model_article = D('ArticleClass');
                    $info          = $model_article->del($ac_id);
                    if ($info >= 0) {
                        admin_log('删除文章分类' . '[' . $ac_id . ']', null);
                        $data = array('info' => '修改成功', 'status' => '1');
                    } else {
                        $data = array('info' => '修改失败', 'status' => '0');
                    }
                }
            } else {
                $data = array('info' => '参数有误');
            }
            $this->ajaxReturn($data, 'json');
        }

    }
    public function detadelclass()
    {
        $model_article = D('ArticleClass');

        $ac_id = $_POST['ac_id'];
        //print_r($ac_id);exit;
        foreach ($ac_id as $key => $val) {

            $info = $model_article->del($val);
            admin_log('批量删除文章分类' . '[' . $val . ']', null);
        }
        if ($info) {
            $this->success('删除文章分类完成', 'cat');exit;
        } else {
            $this->error('删除文章分类失败', 'cat');exit;
        }

    }
}
